package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IProductService;
import com.mdd.admin.validate.ProductCreateValidate;
import com.mdd.admin.validate.ProductSearchValidate;
import com.mdd.admin.validate.ProductUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ProductDetailVo;
import com.mdd.admin.vo.ProductListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Product;
import com.mdd.common.mapper.ProductMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 产品实现类
 * @author LikeAdmin
 */
@Service
public class ProductServiceImpl implements IProductService {
        
    @Resource
    ProductMapper productMapper;

    /**
     * 产品列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ProductListedVo>
     */
    @Override
    public PageResult<ProductListedVo> list(PageValidate pageValidate, ProductSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        productMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:categoryid@category_id:long",
            "like:name:str",
            "=:description:str",
            "=:image:str",
            "=:images:str",
            "=:details:str",
            "=:keywords:str",
            "=:isHot@is_hot:int",
            "=:isNew@is_new:int",
            "=:sort:int",
            "=:status:int",
            "=:views:int",
        });

        IPage<Product> iPage = productMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ProductListedVo> list = new LinkedList<>();
        for(Product item : iPage.getRecords()) {
            ProductListedVo vo = new ProductListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImage(UrlUtils.toAdminAbsoluteUrl(item.getImage()));
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 产品详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Product
     */
    @Override
    public ProductDetailVo detail(Integer id) {
        Product model = productMapper.selectOne(
                new QueryWrapper<Product>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ProductDetailVo vo = new ProductDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setImage(UrlUtils.toAdminAbsoluteUrl(model.getImage()));
        return vo;
    }

    /**
     * 产品新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ProductCreateValidate createValidate) {
        Product model = new Product();
        model.setCategoryId(createValidate.getCategoryid());
        model.setName(createValidate.getName());
        model.setDescription(createValidate.getDescription());
        model.setImage(UrlUtils.toRelativeUrl(createValidate.getImage()));
        model.setImages(createValidate.getImages());
        model.setDetails(createValidate.getDetails());
        model.setKeywords(createValidate.getKeywords());
        model.setIsHot(createValidate.getIshot());
        model.setIsNew(createValidate.getIsnew());
        model.setSort(createValidate.getSort());
        model.setStatus(createValidate.getStatus());
        model.setViews(createValidate.getViews());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        productMapper.insert(model);
    }

    /**
     * 产品编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ProductUpdateValidate updateValidate) {
        Product model = productMapper.selectOne(
                new QueryWrapper<Product>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCategoryId(updateValidate.getCategoryid());
        model.setName(updateValidate.getName());
        model.setDescription(updateValidate.getDescription());
        model.setImage(UrlUtils.toRelativeUrl(updateValidate.getImage()));
        model.setImages(updateValidate.getImages());
        model.setDetails(updateValidate.getDetails());
        model.setKeywords(updateValidate.getKeywords());
        model.setIsHot(updateValidate.getIshot());
        model.setIsNew(updateValidate.getIsnew());
        model.setSort(updateValidate.getSort());
        model.setStatus(updateValidate.getStatus());
        model.setViews(updateValidate.getViews());
        model.setUpdateTime(new Date());
        productMapper.updateById(model);
    }

    /**
     * 产品删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Product model = productMapper.selectOne(
                new QueryWrapper<Product>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        productMapper.delete(new QueryWrapper<Product>().eq("id", id));
    }

}
